// 555电影网的回调
export function Dy555CallBack() {
  let searchEl = document.getElementsByClassName('search-box') || [];
  if (searchEl[0]) {
    searchEl[0].style['align-items'] = 'center';
  }
  if (!document.getElementsByClassName('boxue_bilibili_speedbutton')?.length) {
    setDoubleSpeed(1);
    setDoubleSpeed(1.25);
    setDoubleSpeed(1.5);
    setDoubleSpeed(1.75);
    setDoubleSpeed(2);
    setDoubleSpeed(2.5);
    setDoubleSpeed(3);
    setDoubleSpeed(3.5);
    setDoubleSpeed(4);
    setDoubleSpeed(4.5);
    setDoubleSpeed(5);
    SetSpeed(-10); // 调整进度
    SetSpeed(10);
    SetSpeed(15);
    SetSpeed(20);
    SetSpeed(25);
    SetSpeed(30);
  }

  window.Dy555_Video_speed = 2;
  window.Bilibili_Video_Timer = null;
  window.Bilibili_Video_Timer = setInterval(() => {
    let curiframe = document.querySelector('iframe');
    if (curiframe) {
      let iFramDocument = curiframe.contentDocument;
      let currentVideoEl = iFramDocument.getElementsByTagName('video')[0];
      if (currentVideoEl) {
        currentVideoEl.playbackRate = window.Dy555_Video_speed; // 倍速
      }
    }
  }, 1000);

  setTimeout(() => {
    let curiframe = document.querySelector('iframe');
    if (curiframe) {
      let iFramDocument = curiframe.contentDocument;
      let currentVideoEl = iFramDocument.getElementsByTagName('video')[0];
      if (currentVideoEl) {
        currentVideoEl.playbackRate = 2.0; // 倍速
        currentVideoEl.paused && currentVideoEl?.play(); // 自动播放
      }
    }
  }, 300);
}

function setDoubleSpeed(val) {
  var spanEl = document.createElement('span');
  // spanEl.innerHTML = val + '倍';
  spanEl.innerHTML = val;
  spanEl.className = 'boxue_bilibili_speedbutton';
  spanEl.onclick = () => Dy555SetSpeed(val);
  document.getElementsByClassName('search-box')[0].appendChild(spanEl);
}
// 设置播放速度
function Dy555SetSpeed(val = 2) {
  let curiframe = document.querySelector('iframe');
  if (curiframe) {
    let iFramDocument = curiframe.contentDocument;
    let currentVideoEl = iFramDocument.getElementsByTagName('video')[0];
    if (currentVideoEl) {
      currentVideoEl.playbackRate = val; // 倍速
      window.Dy555_Video_speed = val;
      currentVideoEl.paused && currentVideoEl?.play(); // 自动播放
    }
  }
}
function SetSpeed(val = 10) {
  var spanEl = document.createElement('span');
  spanEl.innerHTML = val + 's';
  spanEl.className = 'boxue_bilibili_speedbutton';
  spanEl.onclick = () => speedOfProgress(val);
  document.getElementsByClassName('search-box')[0].appendChild(spanEl);
}
// 调整进度(秒)
function speedOfProgress(val = 10) {
  let curiframe = document.querySelector('iframe');
  if (curiframe) {
    let iFramDocument = curiframe.contentDocument;
    let currentVideoEl = iFramDocument.getElementsByTagName('video')[0];
    if (currentVideoEl) {
      currentVideoEl.currentTime += val;
    }
  }
}
